//
//  ViewController.m
//  SimplystylePictorial
//
//  Created by Mac on 15/7/6.
//  Copyright (c) 2015年 Hu. All rights reserved.
//

#import "ViewController.h"
#import "Header.h"
#import "progressModel.h"


@interface ViewController ()<MBProgressHUDDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
{
    MBProgressHUD *HUD ;
    UICollectionView *_progressCollectionView;
}
@property (nonatomic,copy)NSMutableArray* progressArr;
@property (nonatomic,copy)NSMutableArray* infoArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];//导航栏返回键字体颜色
    
    HUD = [[MBProgressHUD alloc]initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:HUD];
    HUD.labelText = @"玩命加载中...";
    HUD.delegate = self ;
    [HUD show:YES];
    
    [self getInfoRequest];
    [self createCollectionView];//创建collectionView
    
    self.infoArr = [[NSMutableArray alloc]initWithCapacity:0];
    self.progressArr = [[NSMutableArray alloc]initWithCapacity:0];
}

- (void)getInfoRequest
{
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:@"http://mmmono.com/api/category/program?page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.progressArr = responseObject[@"programs"];//添加programs
        
        NSMutableArray *datas = [[NSMutableArray alloc]initWithCapacity:0];
        for(NSDictionary *dict in self.progressArr)
        {
            progressModel *model = [progressModel setInfoWithDict:dict];
            [datas addObject:model];
        }
        self.infoArr = datas;
        [_progressCollectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        UIAlertView *alterView =[[UIAlertView alloc]initWithTitle:@"sorry,网络发生错误" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alterView show];
    }];
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 10 ;
    layout.minimumLineSpacing = 20;
    layout.itemSize = CGSizeMake((kScreenW/2-30), (kScreenW/2-30)*3/4);
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 0, 20);
    
    _progressCollectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    _progressCollectionView.delegate = self ;
    _progressCollectionView.dataSource = self ;
    _progressCollectionView.backgroundColor = [UIColor clearColor];
    [_progressCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_progressCollectionView];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.progressArr.count;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    progressModel *model = self.infoArr[indexPath.row];
    if(self.infoArr.count != 0 )
    {
        [HUD hide:YES];
    }
    //连接图片
    UIImageView *cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW/2-30, (kScreenW/2-30)*3/4)];
    [cellImageView sd_setImageWithURL:[NSURL URLWithString:model.thumb_url]];
    cellImageView.clipsToBounds = YES ;
    cellImageView.contentMode = UIViewContentModeScaleAspectFill;
    [cell addSubview:cellImageView];
    
    //黑色蒙版
    UIView *cellBoardView = [[UIView alloc]initWithFrame:cellImageView.bounds];
    cellBoardView.backgroundColor = [UIColor blackColor];
    cellBoardView.alpha = 0.7;
    [cellImageView addSubview:cellBoardView];
    
    //小标题的lable
    UILabel *cellLaebel = [[UILabel alloc]initWithFrame:cellImageView.bounds];
    cellLaebel.text = model.name;
    cellLaebel.textAlignment = 1;
    cellLaebel.font = [UIFont systemFontOfSize:20];
    cellLaebel.textColor = [UIColor whiteColor];
    [cellBoardView addSubview:cellLaebel];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ProgramsInfoViewController *programs = [[ProgramsInfoViewController alloc]init];
    programs.hidesBottomBarWhenPushed = YES ;
    progressModel *model = self.infoArr[indexPath.row];
    programs.ID = model.ID;
    programs.title = model.name;
    [self.navigationController pushViewController:programs animated:YES];
}

@end
